package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.domain.ResponseResult;
import com.lagou.domain.Role;
import com.lagou.domain.User;
import com.lagou.domain.UserVO;
import com.lagou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户分页&条件查询
     *
     * @param userVO
     * @return
     */
    @RequestMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVO userVO) {

        PageInfo<User> pageInfo = userService.findAllUserByPage(userVO);
        return new ResponseResult(true, 200, "用户分页&条件查询", pageInfo);
    }

    /**
     * 修改用户状态
     * ENABLE能登录，DISABLE不能登录
     *
     * @param id     用户id
     * @param status 用户状态
     * @return
     */
    @RequestMapping("/updateUserStatus")
    public ResponseResult updateUserStatus(Integer id, String status) {

        if ("ENABLE".equalsIgnoreCase(status)) {

            status = "DISABLE";
        } else {

            status = "ENABLE";
        }

        userService.updateUserStatus(id, status);
        return new ResponseResult(true, 200, "修改用户状态", status);
    }

    /**
     * 用户登录
     *
     * @param user
     * @param request
     * @return
     */
    @RequestMapping("/login")
    public ResponseResult login(User user, HttpServletRequest request) throws Exception {

        Integer state = 400;
        String msg = "用户名密码错误";
        Map<String, Object> map = null;
        User login = userService.login(user);
        if (login != null) {

            String accessToken = UUID.randomUUID().toString();
            HttpSession session = request.getSession();
            session.setAttribute("access_token", accessToken);
            session.setAttribute("user_id", login.getId());
            map = new HashMap<>();
            map.put("access_token", accessToken);
            map.put("user_id", login.getId());
            map.put("user", login);
            state = 1;
            msg = "登陆成功";
        }

        return new ResponseResult(true, state, msg, map);
    }

    /**
     * 分配角色(回显)
     *
     * @param id 用户id
     * @return
     */
    @RequestMapping("/findUserRoleById")
    public ResponseResult findUserRoleById(Integer id) {

        List<Role> list = userService.findUserRoleById(id);
        return new ResponseResult(true, 200, "分配角色(回显)", list);
    }

    /**
     * 分配角色
     *
     * @param userVO
     * @return
     */
    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserVO userVO) {

        userService.userContextRole(userVO);
        return new ResponseResult(true, 200, "分配角色", null);
    }

    /**
     * 获取用户拥有的权限
     * @return
     */
    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request) {

        ResponseResult result=null;
        String token = request.getHeader("Authorization");
        HttpSession session = request.getSession();
        String accessToken = (String) session.getAttribute("access_token");

        if (token.equals(accessToken)) {

            int userId = (int) session.getAttribute("user_id");
            Map<String, Object> map = userService.getUserPermissions(userId);
            result = new ResponseResult(true, 200, "获取用户拥有的权限", map);
        }else{

            result = new ResponseResult(false, 400, "获取失败", null);
        }

        return result;
    }
}
